package com.kaifamiao.response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@WebServlet("/show/time")
public class ShowTimeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置向响应缓冲区中输出数据的字符编码名称
        response.setCharacterEncoding( "UTF-8" );
        // 设置响应缓冲区容量
        response.setBufferSize( 10240 );

        // 设置响应对象中 `content-type` 字段的值 (即将来向HTTP客户端发送的响应数据的MIME类型)
        response.setHeader( "content-type", "text/html;charset=UTF-8" );
        // response.setHeader( "content-type", "text/plain;charset=UTF-8" );
        // 获得向响应缓冲区中输出数据的字符打印流
        PrintWriter out = response.getWriter();
        // 使用 JDK 1.8 提供的 LocalDateTime 获得当前时间
        LocalDateTime datetime = LocalDateTime.now();
        // 指定日期时间的模式
        String pattern = "G yyyy年MM月dd日 E HH点mm分ss秒SSS毫秒" ;
        // 创建日期时间解析/格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern( pattern );
        // 使用 DateTimeFormatter 实例对 日期时间 进行格式化
        String str = formatter.format( datetime );

        out.println( "<H3 style='text-align : center ;'>" );
        out.println( str );
        out.println( "<H3>" );

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        throw new ServletException( "获取个时间用什么POST请求？" );
    }
}
